#include "EndLayer.h"

EndLayer::EndLayer()
: pGameLayerManager(NULL)
{
}

EndLayer::~EndLayer()
{
}

EndLayer * EndLayer::create()
{
  EndLayer * pLayer = new EndLayer();
  if (pLayer && pLayer->init())
  {
    pLayer->autorelease();
    return pLayer;
  }
  else
  {
    CC_SAFE_DELETE(pLayer);
    return NULL;
  }
}

bool EndLayer::init()
{
  do
  {
    CC_BREAK_IF(!CCLayer::init());

    CCLabelTTF *pRetryLabel = CCLabelTTF::create("RETRY", "Arial", 40);
    CC_BREAK_IF(!pRetryLabel);
    CCMenuItemLabel * pRetryItemLabel = CCMenuItemLabel::create(pRetryLabel, this, menu_selector(EndLayer::onRetry));
    CC_BREAK_IF(!pRetryItemLabel);
    pRetryItemLabel->setPositionY(50);

    CCLabelTTF *pExitLabel = CCLabelTTF::create("EXIT", "Arial", 40);
    CC_BREAK_IF(!pExitLabel);
    CCMenuItemLabel * pExitItemLabel = CCMenuItemLabel::create(pExitLabel, this, menu_selector(EndLayer::onExit));
    CC_BREAK_IF(!pExitItemLabel);
    pExitItemLabel->setPositionY(-50);

    CCMenu * pMenu = CCMenu::create(pRetryItemLabel, pExitItemLabel,  NULL);
    CC_BREAK_IF(!pMenu);
    addChild(pMenu);
    return  true;
  }
  while(0);
  return false;
}

void EndLayer::onRetry(CCObject * pObj)
{
  pGameLayerManager->retryGame();
}

void EndLayer::onExit(CCObject * pObj)
{
  pGameLayerManager->exitGame();
}

void EndLayer::setGameLayerManager(GameLayerManager * pGameLayerManager)
{
  this->pGameLayerManager = pGameLayerManager;
}
